/*
 * random.cpp
 *
 * Copyright (C) 2008, 2009
 *
 * Lukasz Bieniasz-Krzywiec
 * Dariusz Leniowski
 */

#include "random.h"
#include <omp.h>

/*
 * Podstawowy generator z jezyka
 * Brak dodatkowych zalozen
 */
random_cpp::random_cpp() {}
random_cpp::~random_cpp() {}

void random_cpp::init() {
  struct timeval tv;
  struct timezone tz;
  gettimeofday(&tv, &tz);
  srand48_r(tv.tv_sec + tv.tv_usec, &buffer);
}

int random_cpp::rand(int n) { return (int) ((real) n * rand()); }

real random_cpp::rand() {
  real result;
  drand48_r(&buffer, &result);
  return result;
}

/* RANDOM PARALLEL CPP */

random_parallel_cpp::random_parallel_cpp()
{
  #pragma omp parallel
  {
    #pragma omp single
    {
      int num_threads = omp_get_num_threads();
      generators = new random_cpp*[num_threads];
      fprintf(stderr, "Utworzono %d generatorow.\n", num_threads);
    }

    generators[omp_get_thread_num()] = new random_cpp();
  }
}

random_parallel_cpp::~random_parallel_cpp()
{
	#pragma omp parallel
	delete generators[omp_get_thread_num()];

	delete generators;
}

void random_parallel_cpp::init()
{
	#pragma omp parallel
	generators[omp_get_thread_num()]->init();
}

int random_parallel_cpp::rand(int n)
{
	return generators[omp_get_thread_num()]->rand(n);
}

real random_parallel_cpp::rand()
{
	return generators[omp_get_thread_num()]->rand();
}

